package org.agony2d.view.supportClasses 
{
	import flash.display.MovieClip;
	import org.agony2d.notify.Observer;
	import org.agony2d.notify.AEvent;
	import org.agony2d.view.Fusion;
	import org.agony2d.view.puppet.DisplayObjectContainerPuppet;
	
	import org.agony2d.core.agony_internal;
	use namespace agony_internal;
	
	/**
	 * @usage
	 * 
	 * @property
	 * 		1. ◇effectEnabled
	 * 
	 * @method
	 * 		1. ◆addReleaseEffectListener
	 * 		2. ◆addPressEffectListener
	 * 		3. ◆addLeaveEffectListener
	 * 		4. ◆addHoverEffectListener
	 * 		5. ◆addInvalidEffectListener
	 * 		6. ◆resetButton
	 */
public class AbstractButton extends Fusion 
{

	public function AbstractButton()
	{
		super()
		this._addListeners()
	}
	
	
	/** ◇是否启用按钮效果 **/
	final public function get effectEnabled() : Boolean { return m_effectEnabled }
	final public function set effectEnabled( b:Boolean ) : void
	{
		m_effectEnabled = b
		
		if (!b && m_releaseListener)
		{
			m_releaseListener.execute(); 
		}
	}
	
	
	/** ◆弹起效果 **/
	final public function addReleaseEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_releaseListener ||= Observer.getObserver()).addListener(listener, priority);
	}
	
	/** ◆按下效果 **/
	final public function addPressEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_pressListener ||= Observer.getObserver()).addListener(listener, priority);
	}
	
	/** ◆滑出效果 **/
	final public function addLeaveEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_leaveListener ||= Observer.getObserver()).addListener(listener, priority);
	}
	
	/** ◆滑上效果 **/
	final public function addHoverEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_hoverListener ||= Observer.getObserver()).addListener(listener, priority);
	}
	
	/** ◆失效效果 **/
	final public function addInvalidEffectListener( listener:Function, priority:int = 0 ) : void
	{
		(m_invalidListener ||= Observer.getObserver()).addListener(listener, priority);
	}
	
	/** ◆重置按钮 **/
	final public function resetButton() : void
	{
		if (this.interactive || !m_invalidListener)
		{
			if (m_releaseListener)
			{
				m_releaseListener.execute();
			}
		}
		
		else
		{
			m_invalidListener.execute()
		}
	}
	
	
	
	agony_internal function _addListeners() : void
	{
		this.addEventListener(AEvent.LEAVE,              leaveEffect)
		this.addEventListener(AEvent.RELEASE,            releaseEffect)
		this.addEventListener(AEvent.PRESS,              pressEffect)
		this.addEventListener(AEvent.OVER,               hoverEffect)
		this.addEventListener(AEvent.INTERACTIVE_CHANGE, invalidEffect)
	}
	
	override agony_internal function dispose() : void
	{
		if (m_releaseListener)
		{
			m_releaseListener.dispose();
			m_releaseListener = null;
		}
		if (m_pressListener)
		{
			m_pressListener.dispose();
			m_pressListener = null;
		}
		if (m_leaveListener)
		{
			m_leaveListener.dispose();
			m_leaveListener = null;
		}
		if (m_hoverListener)
		{
			m_hoverListener.dispose();
			m_hoverListener = null;
		}
		if (m_invalidListener)
		{
			m_invalidListener.dispose()
			m_invalidListener = null
		}
		super.dispose();
	}
	
	agony_internal static function pressEffect( e:AEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (btn.effectEnabled && btn.m_pressListener)
		{
			btn.m_pressListener.execute()
		}
	}
	
	agony_internal static function releaseEffect( e:AEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (btn.effectEnabled && btn.m_releaseListener)
		{
			btn.m_releaseListener.execute()
		}
	}
	
	agony_internal static function leaveEffect( e:AEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (btn.effectEnabled && btn.m_leaveListener)
		{
			btn.m_leaveListener.execute()
		}
	}
	
	agony_internal static function hoverEffect( e:AEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (btn.effectEnabled && btn.m_hoverListener)
		{
			btn.m_hoverListener.execute()
		}
	}
	
	agony_internal static function invalidEffect( e:AEvent ) : void
	{
		var btn:AbstractButton = e.target as AbstractButton;
		
		if (btn.interactive || !btn.m_invalidListener)
		{
			if (btn.m_releaseListener)
			{
				btn.m_releaseListener.execute()
			}
		}
		
		else
		{
			btn.m_invalidListener.execute()
		}
	}
	
	
	agony_internal var m_releaseListener:Observer, m_pressListener:Observer, m_leaveListener:Observer, m_hoverListener:Observer
	
	agony_internal var m_invalidListener:Observer;
	
	agony_internal var m_effectEnabled:Boolean = true
}
}